<?php
include('header.php');
//invalid URL, redirect to index page
if (!$session->isAdmin())
{
	header("Location: ../index.php");
} 
else 
{
	if (!isset($_GET['id'])) 
	{ 
		//header("Location: ".$config['WEB_ROOT'].$config['home_page']);
		header("Location: main.php?id=4");
	}
	else if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
	{
		$orgID = $_GET['id'];
		$reg_org_info = $database->getOrgInfo($orgID);?>

<div id="form_container">
  <div id="title_container">
    <div id="title">
      <p>Edit <?php echo $reg_org_info['orgName'] ?></p>
    </div>
    <div id="title_link"> </div>
  </div>
  <div id="form_content">
    <?php if($form->num_errors > 0)
        { ?>
    <div id="form_error">
      <?php $form->num_errors ?>
      error(s) found </div>
    <?php } ?>
    <ul>
      <form action="adminprocess.php" method="post">
        <fieldset>
          <legend>Organization Information</legend>
          <li id="li_orgName">
            <label id="label_orgName" for="orgName">Organization Name <?php echo $form->error("orgName"); ?></label>
            <input id="orgName" name="orgName" type="text" size="30" maxlength="30" 
                value="<?php if($form->value("orgName") == ""){echo $reg_org_info['orgName'];}else{echo $form->value("orgName");}?>" />
          </li>
          <li id="li_websiteAddress">
            <label id="label_websiteAddress" for="websiteAddress">Website Address <?php echo $form->error("websiteAddress"); ?></label>
            <input class="large" id="websiteAddress" name="websiteAddress" type="text" size="40" maxlength="250" 
                value="<?php if($form->value("websiteAddress") == ""){echo $reg_org_info['websiteAddress'];}else{echo $form->value("websiteAddress");}?>" />
          </li>
          <li id="li_orgName">
            <label id="label_orgName" for="streetAddress">Street Address</label>
            <input id="streetAddress" name="streetAddress" type="text" size="30" maxlength="30" 
                value="<?php if($form->value("streetAddress") == ""){echo $reg_org_info['streetAddress'];}else{echo $form->value("streetAddress");}?>" />
            <br />
            <input id="streetAddress2" name="streetAddress2" type="text" size="30" maxlength="30" 
                value="<?php if($form->value("streetAddress2") == ""){echo $reg_org_info['streetAddress2'];}else{echo $form->value("streetAddress2");}?>" />
            <br />
            <input id="city" name="city" type="text" size="20" maxlength="30" 
                value="<?php if($form->value("city") == ""){echo $reg_org_info['city'];}else{echo $form->value("city");}?>" />
            <input id="state" name="state" type="text" size="5" maxlength="30" 
            	value="<?php if($form->value("state") == ""){echo $reg_org_info['state'];}else{echo $form->value("state");}?>" />
            <input id="zipCode" name="zipCode" type="text" size="10" maxlength="30" 
            	value="<?php if($form->value("zipCode") == ""){echo $reg_org_info['zipCode'];}else{echo $form->value("zipCode");}?>" />
          </li>
          <li id="li_category">
            <label id="label_orgName" for="category">Category</label>
            <?php function org_type_select($default_value='') 
			{
				$select = '<select name="category">';
				$options = array('other','farm','farmers_market','community_garden','orchard','csa',);
				foreach($options as $option) 
				{
					$display;
					switch($option)
					{
						case "other":
							$display = "Other";
							break;
						case "farm":
							$display = "Farm";
							break;
						case "farmers_market":
							$display = "Farmer's Market";
							break;
						case "community_garden":
							$display = "Community Garden";
							break;
						case "orchard":
							$display = "Orchard";
							break;
						case "csa":
							$display = "CSA";
							break;
					}
					$select .= write_option($option, $display, $default_value);
				}
				$select .= '</select>';
				return $select;  
			}
			
			function write_option($value, $display, $default_value='') 
			{
				$option = '<option value="'.$value.'"';
				$option .= ($default_value == $value) ? ' SELECTED' : '';
				$option .= '>'.$display.'</option>';
				return $option;
			}
		 	echo org_type_select($reg_org_info['category']); ?>
          </li>
        </fieldset>
        <fieldset>
          <legend>Contact Information</legend>
          <li id="li_email">
            <label id="label_email">Email <?php echo $form->error("email"); ?></label>
            <input id="email" name="email" type="text" size="30" maxlength="30" 
            	value="<?php if($form->value("email") == ""){echo $reg_org_info['email'];}else{echo $form->value("email");}?>" />
          </li>
          <li id="li_phoneNumber">
            <label id="label_phoneNumber">Phone Number</label>
            <input id="phoneNumber" name="phoneNumber" type="text" size="20" maxlength="20" placeholder="(___) ___-____"
            	value="(<?php echo $reg_org_info['areaCode'];?>) <?php echo $reg_org_info['prefix'];?>-<?php echo $reg_org_info['lineNumber'];?>" />
          </li>
          <li id="li_orgName">
            <label id="label_faxNumber">Fax Number</label>
            <input id="faxNumber" name="faxNumber" type="text" size="20" maxlength="20" placeholder="(___) ___-____"
            	value="(<?php echo $reg_org_info['faxAreaCode'];?>) <?php echo $reg_org_info['faxPrefix'];?>-<?php echo $reg_org_info['faxLineNumber'];?>" />
          </li>
        </fieldset>
        <li class="button">
          <input type="hidden" name="subeditorg" value="1">
          <input type="hidden" name="orgID" value="<?php echo $orgID; ?>">
          <input style="width:150px;" class="submit" type="submit" name="button" value="Edit Organization">
          <input style="width:150px;" class="submit" type="submit" name="button" value="Delete Organization" onclick="return confirm ('Are you sure you want to delete this organization, this cannot be undone?\n\n' + 'Click OK to continue or Cancel to Abort!')">
        </li>
      </form>
    </ul>
  </div>
</div>
<!-- live typecasting -->
<script type="text/javascript" src="../jscripts/jquery.maskedinput-1.3.js" ></script>
<script>
jQuery(function($){
   $("#phoneNumber").mask("(###) ###-####");
   $("#faxNumber").mask("(###) ###-####");
});
</script>
<?php }
}
include('footer.php');?>
